<template>
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="发票编号" prop="no">
        <el-input v-model="queryParams.no" placeholder="请输入发票编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开票单位" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入开票单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目单位" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入项目单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务员" prop="saleName">
        <el-input v-model="queryParams.saleName" placeholder="请输入业务员" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="制单人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入制单人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
              show-summary :summary-method="getSummaries">
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="发票编号" align="center" prop="no" />
      <el-table-column label="开票单位" align="center" prop="customerName" show-overflow-tooltip />
      <el-table-column label="订单编号" align="center" prop="projectNo" show-overflow-tooltip />
      <el-table-column label="业务员" align="center" prop="saleName" show-overflow-tooltip />
      <el-table-column label="制单人" align="center" prop="createBy" show-overflow-tooltip />
      <el-table-column label="申请人" align="center" prop="drawerName" />
      <el-table-column label="核销金额" align="center" prop="pinMoney" />
      <el-table-column label="核销日期" align="center" prop="pinDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="hexiaomingxi(scope.row)"
            v-hasPermi="['develop:GoodsInvoiceDetail:edit']">核销明细</el-button>
          <el-popconfirm :title="`确认取消该核销项？`" @confirm="handleCancel(scope.row)"
                                   style="margin-left: 10px;">
                        <el-button slot="reference" size="mini" type="text" icon="el-icon-delete">取消核销</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <GoodsInvoiceDetail ref="formHxDeatil" @ok="getList" />
  </el-dialog>
</template>

<script>
import {
  accAdd,
  deleteAction,
  getAction
} from '@/api/manage'
  import GoodsInvoiceDetail from '@/views/develop/goodsInvoiceDetail/module/GoodsInvoiceDetail'

  export default {
    name: "GoodsInvoiceDetailModuleList",
    components: {
      GoodsInvoiceDetail,
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 是否显示弹出层
        open: false,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 核销明细信息管理表格数据
        dataList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          invoiceId: null,
          pinMoney: null,
          pinDate: null,
          saleId: null,
          saleName: null,
          status: null,
          orderByColumn: "a.pin_date",
          isAsc: "desc",
          no: null,
          customerName: null,
          projectNo: null,
          drawerName: null,
          companyName: null,
          createBy: null,
          params: {}
        },
        url: {
          list: '/goodsInvoiceDetail/goodsInvoiceDetail/listMingxi',
          remove: '/goodsInvoiceDetail/goodsInvoiceDetail/',
          export: '/goodsInvoiceDetail/goodsInvoiceDetail/export',
          cancelHexiao: '/goodsInvoiceDetail/goodsInvoiceDetail/cancelHexiao/',
        },
      };
    },
    created() {
    },
    methods: {
      hexiaomingxi(row) {
        this.$refs.formHxDeatil.handleUpdate(row);
      },

      /** 查询核销明细信息管理列表 */
      getList() {
        this.loading = true;
        this.queryParams.invoiceId = this.invoiceId;
        getAction(this.url.list, this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.params = {}
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.form.handleAdd();
      },

      detailList(row){
        this.open = true;
        this.title = "核销记录";
        this.queryParams.invoiceId = row.id;
        this.invoiceId = row.id;
        this.getList();
      },


      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs.form.handleUpdate(row);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
          this.$emit('ok');
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(this.url.export, {
          ...this.queryParams
        }, `goodsInvoiceDetail_${new Date().getTime()}.xlsx`)
      },
      getSummaries(param) {
        // console.log(param);
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = "合计";
            return;
          }
          const values = data.map((item) => Number(item[column.property]));
          // 只合计amount1和amount3，想合并哪些列就在这里指定
          if ( column.property === "pinMoney") {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return accAdd(prev , curr);
              } else {
                return prev;
              }
            }, 0);
          }
        });
        return sums;
      },

      handleCancel(row){
        const ids = row.id || this.ids;
        getAction(this.url.cancelHexiao + ids, {}).then(response => {
          this.getList();
          this.$modal.msgSuccess("取消核销成功");
          this.open = false;
          this.$emit('ok');
        }).catch(() => {
        });


      }







    }
  };
</script>
